﻿using System;
using Microsoft.Win32;

namespace ProxyToggle
{   
    // ref: http://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C

    public class ModifyRegistry
    {
        private readonly RegistryKey _baseRegistryKey = Registry.CurrentUser;

        public string Read(string subKey, string keyName)
        {
            // Opening the registry key
            RegistryKey rk = _baseRegistryKey;
            
            // Open a subKey as read-only
            RegistryKey sk1 = rk.OpenSubKey(subKey);

            // If the RegistrySubKey doesn't exist -> (null)
            if (sk1 == null)
            {
                return null;
            }
            else
            {
                try
                {
                    // If the RegistryKey exists I get its value
                    // or null is returned.
                    return (string)sk1.GetValue(keyName.ToUpper());
                }
                catch (Exception e)
                {
                    throw new NotImplementedException();
                    return null;
                }
            }
        }

        public bool Write(string KeyName, object Value, string subKey)
        {
            try
            {
                // Setting
                RegistryKey rk = _baseRegistryKey;
                RegistryKey sk1 = rk.CreateSubKey(subKey);
                
                // Save the value
                sk1.SetValue(KeyName, Value);

                return true;
            }
            catch (Exception e)
            {
                throw new NotImplementedException();
                return false;
            }
        }
        public bool DeleteKey(string KeyName, string subKey)
        {
            try
            {
                // Setting
                RegistryKey rk = _baseRegistryKey;
                RegistryKey sk1 = rk.CreateSubKey(subKey);

                // If the RegistrySubKey doesn't exists -> (true)
                if (sk1 == null)
                    return true;
                else
                    sk1.DeleteValue(KeyName);

                return true;
            }
            catch (Exception e)
            {
                throw new NotImplementedException();
                return false;
            }
        }
        public bool DeleteSubKeyTree(string subKey)
        {
            try
            {
                // Setting
                RegistryKey rk = _baseRegistryKey;
                RegistryKey sk1 = rk.OpenSubKey(subKey);
              
                if (sk1 != null)
                    rk.DeleteSubKeyTree(subKey);

                return true;
            }
            catch (Exception e)
            {
                throw new NotImplementedException();
                return false;
            }
        }
        public int SubKeyCount(string subKey)
        {
            try
            {
                // Setting
                RegistryKey rk = _baseRegistryKey;
                RegistryKey sk1 = rk.OpenSubKey(subKey);
               
                // If the RegistryKey exists...
                if (sk1 != null)
                    return sk1.SubKeyCount;
                else
                    return 0;
            }
            catch (Exception e)
            {
                throw new NotImplementedException();
                return 0;
            }
        }
        public int ValueCount(string subKey)
        {
            try
            {
                // Setting
                RegistryKey rk = _baseRegistryKey;
                RegistryKey sk1 = rk.OpenSubKey(subKey);

                // If the RegistryKey exists...
                if (sk1 != null)
                    return sk1.ValueCount;
                else
                    return 0;
            }
            catch (Exception e)
            {
                throw new NotImplementedException();
                return 0;
            }
        }
    }
}
